package com.zmn.oms.zmn.normal.business.impl.work.customwork;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.comment.OrderCommentBService;
import com.zmn.oms.business.interfaces.remark.OrderRemarkBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.MapDTO;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.dto.comment.OrderCommentDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDetailDTO;
import com.zmn.oms.model.dto.work.modify.must.ZsReplyVisitDTO;
import com.zmn.oms.model.entity.comment.OrderComment;
import com.zmn.oms.model.entity.comment.OrderCommentDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.modify.must.ZsReplyVisitVO;
import com.zmn.oms.services.interfaces.comment.OrderCommentService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.services.interfaces.work.customwork.CustomWorkService;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsCustomWorkBService;
import com.zmn.oms.zmn.normal.business.interfaces.order.ZsNormalOrderBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.customwork.ZsNormalCustomWorkBService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：客服工单业务实现
 *
 * @author liuying
 * @date 2018/11/02 21:02
 */
@Service
public class ZsNormalCustomWorkBServiceImpl implements ZsNormalCustomWorkBService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ZsNormalOrderBService zsNormalOrderBService;
    @Autowired
    protected OrderCommentService orderCommentService;
    @Autowired
    protected OrderCommentBService orderCommentBService;
    @Autowired
    protected OrderWorkService orderWorkService;
    @Autowired
    protected CustomWorkService customWorkService;
    @Autowired
    protected OrderRemarkBService orderRemarkBService;
    @Autowired
    protected ZsCustomWorkBService zsCustomWorkBService;

    @Override
    public ZsReplyVisitVO findReplyVisit(Long orderId, Long workId) {
        ZsReplyVisitVO replyVisitVO = new ZsReplyVisitVO();
        replyVisitVO.setOrderId(orderId);
        replyVisitVO.setWorkId(workId);
        replyVisitVO.setCommentStatus(GlobalConsts.NO);

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        replyVisitVO.setShouldComment(orderWork.getCompleteTime() != null ? GlobalConsts.YES : GlobalConsts.NO);
        replyVisitVO.setBizLine(orderWork.getBizLine());
        replyVisitVO.setBizType(orderWork.getBizType());

        OrderComment comment = orderCommentService.findCommentByKey(orderId);
        if (comment != null && Objects.equals(comment.getStatus(), GlobalConsts.YES)) {
            replyVisitVO.setScore(comment.getScore());

            replyVisitVO.setCommentStatus(comment.getStatus());

            // 拼接评价内容
            replyVisitVO.setCommentContent(StringUtils.defaultString(comment.getContent()));

            // 拼接评价detail内容
            List<OrderCommentDetail> orderCommentDetailList = comment.getDetailList();
            StringBuilder commentDetailStr = new StringBuilder();
            orderCommentDetailList.forEach(detail -> {
                commentDetailStr.append(detail.getMapName());
                commentDetailStr.append(",");
            });
            commentDetailStr.append(StringUtils.defaultString(comment.getContent()));
            if (commentDetailStr.length() > 0) {
                replyVisitVO.setCommentContent(commentDetailStr.toString());
            }
        }
        return replyVisitVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_REPLYVISIT)
    public void saveReplyVisit(ZsReplyVisitDTO replyVisitDTO) throws Exception {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(replyVisitDTO.getOrderId(), replyVisitDTO.getWorkId());

        // 如果之前未评价 保存评价信息
        if (orderWork.getCompleteTime() != null) {
            OrderComment comment = orderCommentService.findCommentByKey(replyVisitDTO.getOrderId());
            if (comment == null || Objects.equals(comment.getStatus(), GlobalConsts.NO)) {

                // 调用工单评价接口
                OrderCommentDTO orderCommentDTO = BeanMapper.map(replyVisitDTO, OrderCommentDTO.class);
                orderCommentDTO.setContent(replyVisitDTO.getCommentContent());
                orderCommentDTO.setScore(replyVisitDTO.getScore());
                orderCommentDTO.setType(OrderConsts.COMMENT_TYPE_REPLYVISIT);
                if (!CollectionUtils.isEmpty(replyVisitDTO.getCommentCodeMapList())) {
                    List<MapDTO> mapDTOList = Lists.newArrayList();
                    for (KVDict kvDict : replyVisitDTO.getCommentCodeMapList()) {
                        MapDTO mapDTO = new MapDTO();
                        mapDTO.setMapId(Integer.valueOf((String)kvDict.getValue()));
                        mapDTO.setMapName(kvDict.getText());
                        mapDTOList.add(mapDTO);
                    }
                    orderCommentDTO.setCodeMapList(mapDTOList);
                }
                orderCommentBService.saveComment(orderCommentDTO);
            }
        }


        // 修改工单回访
        OrderWork updateOrderWork = new OrderWork();
        updateOrderWork.setOrderId(replyVisitDTO.getOrderId());
        updateOrderWork.setWorkId(replyVisitDTO.getWorkId());
        updateOrderWork.setUpdater(replyVisitDTO.getOperator());
        updateOrderWork.setReplyVisiterId(replyVisitDTO.getOperatorId());
        updateOrderWork.setReplyVisitTime(DateUtil.getNow());
        customWorkService.updateReplyVisit(updateOrderWork);

        // 保存回访数据
        OrderRemarkDTO orderRemarkDTO = new OrderRemarkDTO();
        orderRemarkDTO.setOrderId(replyVisitDTO.getOrderId());
        orderRemarkDTO.setWorkId(replyVisitDTO.getWorkId());
        orderRemarkDTO.setContent(replyVisitDTO.getReplyContent());
        orderRemarkDTO.setOperator(replyVisitDTO.getOperator());
        orderRemarkDTO.setOperatorId(replyVisitDTO.getOperatorId());

        List<OrderRemarkDetailDTO> detailDTOList = Lists.newArrayList();
        List<KVDict<Integer>> replyVisitList = replyVisitDTO.getReplyVisitList();
        replyVisitList.forEach(codeMap -> {
            OrderRemarkDetailDTO detailDTO = new OrderRemarkDetailDTO();
            detailDTO.setMapId(codeMap.getValue());
            detailDTO.setMapName(codeMap.getText());
            detailDTO.setMapValue(codeMap.getExt1());
            detailDTO.setCategory(replyVisitDTO.getItemId());
            detailDTOList.add(detailDTO);
        });
        orderRemarkDTO.setDetailDTOList(detailDTOList);

        // 保存回访数据
        orderRemarkBService.insertOrderRemark(orderRemarkDTO, OrderConsts.ORDER_REMARK_TYPE_REPLY);
    }

}